package com.xy.sql.xml.kit;

import com.xy.sql.xml.core.XySQLResult;
import com.xy.sql.xml.itf.IXySQLLog;
import com.xy.sql.xml.itf.XySQLRegister;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 标准钉钉
 */
public class XySQLPrint {

    /**
     * 打印格式化
     *
     * @param sql
     * @return
     */
    public static String format(String sql) {
        String nowSql = sql.trim();
        while (nowSql.indexOf("  ") > 0) {
            nowSql = nowSql.replaceAll("  ", " ");
        }
        nowSql += " ";
        StringBuilder s = new StringBuilder("##########################################################################");

        char c;
        char nextC;
        int padd = 0;
        for (int index = 0; index < nowSql.length(); index++) {
            c = nowSql.charAt(index);
            if (c == '<') {
                nextC = nowSql.charAt(index + 1);
                if (nextC == '/') {
                    padd = padd - 4;
                    s.append("\n").append(append(padd));
                    s.append(c);
                } else if (nextC == '!') {
                    s.append("\n").append(append(padd));
                    s.append(c);
                } else {
                    s.append("\n").append(append(padd));
                    padd = padd + 4;
                    s.append(c);
                }

            } else if (c == '>') {
                s.append(c).append("\n");
            } else {
                s.append(c);
            }

        }
        return s.toString();
    }

    /**
     * 添加空格
     *
     * @param len
     * @return
     */
    private static String append(int len) {
        StringBuilder s = new StringBuilder();
        for (int index = 0; index < len; index++) {
            s.append("*");
        }
        return s.toString();
    }

    /**
     * 参数打印
     *
     * @param vo
     * @return
     */
    public static void printSql(XySQLResult vo) {
        XySQLRegister.getLogger().info(vo.getSql().trim());
        Object value;
        SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        StringBuilder sqlPara = new StringBuilder();
        for (int i = 0; i < vo.getVoList().size(); i++) {
            value = vo.getVoList().get(i);
            if (value != null && value instanceof Date) {
                sqlPara.append(f1.format((Date) value)).append(" ");
            } else {
                sqlPara.append(value).append(" ");
            }
        }
        XySQLRegister.getLogger().info(sqlPara.toString());
    }


    /**
     * 参数打印
     *
     * @param list
     * @return
     */
    public static void printDetailResult(List<Map<String, Object>> list) {
        if (list == null) {
            return;
        }
        //打印日志
        StringBuilder result = new StringBuilder();
        for (Map<String, Object> item : list) {
            int i = 0;
            for (Map.Entry<String, Object> entry : item.entrySet()) {
                if (i > 0) {
                    result.append(",   ");
                }
                i++;
                result.append(entry.getValue());
            }
            result.append("\r\n");
        }
        XySQLRegister.getLogger().info(result.toString());


    }
}
